﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Web;

namespace SMS_Gateway
{
    public class BulkSMSGateway : HTTPGateway
    {
        public override bool SendSingleSMSTo(Credentials cred, SingleSMS sms, SMSContact cont, bool unicode)
        {
            string prefix = "http://www.bulksms.co.uk:5567/eapi/submission/send_sms/2/2.0";
            var usernameField = new KeyValuePair<string, string>("username", HttpUtility.UrlEncode(cred.Username));
            var passwordField = new KeyValuePair<string, string>("password", HttpUtility.UrlEncode(cred.Password));
            var recepientField = new KeyValuePair<string, string>("msisdn", "2"+cont.Number);

            /*******************
             * Important : The routing group differs in prices in this service , i will be using the cheapest , no. 1 
             * TODO: put that in the settings , somehow
            */
            var routingGroupField = new KeyValuePair<string, string>("routing_group", "1");

            KeyValuePair<string, string> messageField;
            KeyValuePair<string, string> dcaField;




            if (unicode)
            {

                messageField = new KeyValuePair<string, string>("message", HttpUtility.UrlEncodeUnicode(sms.SMS_Body));
                dcaField = new KeyValuePair<string, string>("dca", "16bit");

            }
            else
            {

                messageField = new KeyValuePair<string, string>("message", HttpUtility.UrlEncode(sms.SMS_Body));
                dcaField = new KeyValuePair<string, string>("dca", "7bit");

            }


            string finalQuery = this.BuildQueryString(prefix, new KeyValuePair<string, string>[] { usernameField, passwordField,recepientField, messageField, dcaField, routingGroupField });
            string result = this.HTTPSend(finalQuery);
            if (result.StartsWith("HTTP Error"))
            {
                return true;
            }
            else
            {
                return false;
            }



        }

        public override bool IsConnected
        {
            get
            {
                try
                {
                    HttpWebRequest reqFP = (HttpWebRequest)HttpWebRequest.Create("http://www.bulksms.co.uk:5567/eapi/submission/send_sms/2/2.0");

                    HttpWebResponse rspFP = (HttpWebResponse)reqFP.GetResponse();
                    if (HttpStatusCode.OK == rspFP.StatusCode)
                    {
                        // HTTP = 200 - Internet connection available, server online
                        rspFP.Close();
                        return true;
                    }
                    else
                    {
                        // Other status - Server or connection not available
                        rspFP.Close();
                        return false;
                    }
                }
                catch (WebException)
                {
                    // Exception - connection not available
                    return false;
                }

            }
        }
    }
}
